<?php
namespace Admin\Model;
use Think\Model;
/**
 * 部门管理模型
 *
 */
class SectorModel extends Model{
    const ROOT_SECTOR = 1; /*部门ID为1的就是总公司*/
    const STATE_DEL = 1;
    const STATE_OK = 0;

    public function sub_sector( $sector_id, $include_me = true ){
        $wheres = array();
        $wheres['_string'] = '1=1';
        if( !$include_me )
            $wheres['sector_id '] = array('neq', $sector_id );
        $wheres['is_deleted '] = array('neq', 1);
        $seclist = $this->field("sector_id,parent_sector")->where( $wheres )->select();
        $treelist = list_to_tree($seclist, 'sector_id', 'parent_sector','_child', $sector_id );
        $arrlist = tree_to_list( $treelist, '_child');
        $seclist = array();
        foreach( $arrlist as $vc ){
            $seclist[] = $vc['sector_id'];
        }
        if( $include_me )
            $seclist[] = $sector_id;

        $treelist = null;
        $arrlist = null;

        return $seclist;
    }

    public function add_sector( $sector_name, $parent_sector, $info = '', $manager_id = FALSE ){
        $r = array('r'=> 0,'d'=>NULL,'i'=>'Server Busy!' );
        if( !$sector_name || !$parent_sector ){
            $r['i'] = L("PARAM_WRONG");
        }
        else{
            $wheres = array();
            $wehres['parent_sector'] = array('eq', $parent_sector);
            $wheres['sector_name'] = array('eq', $sector_name );
            $find = $this->where( $wheres )->find();
            if( $find ){
                $r['i'] = L('SECTOR_EXIST');
                $r['d'] = $find;
            }
            else{
                $d = array();
                $d['sector_name'] = $sector_name;
                $d['parent_sector'] = $parent_sector;
                $d['info'] = $info;
                if( $parent_sector == self::ROOT_SECTOR )
                    $d['is_subcompany'] = 1;
                else{
                    $d['is_subcompany'] = 0;
                }
                if( $manager_id )
                    $d['manager_id'] = $manager_id;
                $d['parent_company'] = self::parent_company( $d['parent_sector']);
                $r['d'] = $this->data( $d )->add();
                if( $r['d']){
                    $r['r'] = 1;
                    $r['i'] = 'OK';
                    //如果新添加部门是分公司，那么他们的所属分公司就是节点本身
                    if( 1 == $d['is_subcompany'] ){
                        $d = array();
                        $d['parent_company'] = 1;
                        $wheres = array();
                        $wheres['sector_id'] = array('eq',$r['d']);
                        $this->where( $wheres )->save( $d );
                    }
                }else{
                    $r['i'] = L('DATA_SAVE_ERR');
                }
            }
        }
        return $r;
    }

    public function edit_sector( $sector_id, $sector_name, $parent_sector, $info = '', $manager_id = FALSE ){
        $r = array('r'=> 0,'d'=>NULL,'i'=>'Server Busy!' );
        if( !$sector_id || !$sector_name || !$parent_sector ){
            $r['i'] = L("PARAM_WRONG");
        }
        else{
            $wheres = array();
            $wheres['sector_id'] = array('eq', $sector_id );
            $dfind = $this->where( $wheres )->find();
            if( $dfind ){
                $d = array();
                if( $sector_id == self::ROOT_SECTOR || $dfind['parent_sector'] == $parent_sector){
                    $d['sector_name'] = $sector_name;
                    $d['manager_id'] = $manager_id;
                    $d['info'] = $info;
                    $this->where( $wheres)->save( $d );
                    if( !$this->getError() ){
                        $r['r'] = 1;
                        $r['i'] = L('DATA_SAVE_OK');
                    }else{
                        $r['i'] = L("DATA_UPDATE_ERR");
                    }
                }
                else{
                    $wherep = array();
                    $wherep['parent_sector'] = array('eq', $sector_id);
                    $wherep['is_deleted'] =  array('neq', self::STATE_DEL );
                    $ic = $this->where( $wherep )->count();
                    if( $ic ){
                        $dsec = array();
                        $dsec['parent_sector'] = $dfind['parent_sector'];
                        $this->where( $wherep )->save( $dsec);
                    }
                    $d = array();
                    $d['sector_name'] = $sector_name;
                    $d['parent_sector'] = $parent_sector;
                    $d['manager_id'] = $manager_id;
                    $d['info'] = $info;
                    $r['d'] = $this->where( $wheres )->save( $d );
                    $r['r'] = 1;
                    $r['i'] = 'OK';
                }

            }else{
                $r['i'] = L("NO_DATA");
            }
        }

        return $r;
    }

    public function remove_sector( $sector_id ){
        $r = array('r'=> 0,'d'=>NULL,'i'=>'Server Busy!' );
        $wheres = array();
        $wheres['sector_id'] = array('eq', $sector_id);
        $dfind = $this->where( $wheres )->find();
        if( !$dfind ){
            $r['i'] = "WRONG";
        }
        else{
            if( $dfind['is_deleted'] == self::STATE_DEL ){
                $r['r'] = 1;
                $r['i'] = 'DOWN';
            }
            else{
                $wherep = array();
                $wherep['is_deleted'] = array('neq', self::STATE_DEL );
                $wherep['parent_sector'] = array('eq', $sector_id);
                $ic = $this->where( $wherep )->count();
                if(  $ic ){
                    $dsec = array();
                    $dsec['parent_sector'] = $dfind['parent_sector'];
                    $r['d'] = $this->where( $wherep )->save( $dsec );
                }

                $d = array();
                $d['is_deleted'] = 1;
                $r['d'] = $this->where( $wheres )->save( $d );

                $r['r'] = 1;
                $r['i'] = 'DOWN';
            }
        }
        return $r;
    }

    /**
     * 根据部门ID获取部门所属分公司
     * @param  int $sector_id  部门ID
     * @return int/false       返回指定部门的分公司/不存在就返回false
     */
    public static function parent_company( $sector_id ){
        if( !$sector_id || $sector_id < 1 )
            return false;
        else if( $sector_id == self::ROOT_SECTOR )
            return 0;
        else{
            $Mdo = M("Sector");
            $wheres = array();
            $wheres['sector_id'] = array('eq', $sector_id );
            $dfind = $Mdo->where( $wheres )->find();
            if( !$dfind )
                return false;
            else{
                if( 1 == $dfind['is_subcompany'] )
                    return $sector_id;
                else{
                    return $dfind['parent_company'];
                }
            }
        }
    }

    public static function _parent( $parent_id,$refer ){
        if( isset( $refer[$parent_id]) ){
            if( 1 == $refer[$parent_id]['is_subcompany'])
                return $refer[$parent_id]['sector_id'];
            else if( self::ROOT_SECTOR == $refer[$parent_id]['sector_id'] ||  self::ROOT_SECTOR == $refer[$parent_id]['parent_sector'] )
                return false;
            else
                return self::_parent( $refer[$parent_id]['parent_sector'],$refer );
        }
        else
            return false;
    }
}
?>